<?php
/**
 * Picture represents an picture that can be bought by an account 
 */
class Picture extends Model {
	
	public $id;
	public $account_id;
	public $picture;
	public $timestamp;
	public $is_bought;

	/**
	 * constructs parent
	 */	
	function __construct() {
		parent::__contruct();
	}	
	
	/**  
	 * Gets an Picture by given id
	 * @param picture_id The id of the picture
	 * @return Picture 
	 */		
	function get($picture_id) {	
		$sth = $this->db->prepare('SELECT p.* FROM picture p WHERE p.id = :picture_id');
		$sth->setFetchMode(PDO::FETCH_INTO, $this);	
		$sth->execute(array('picture_id' => $picture_id));
		return $sth->fetch();			
	}
	
	/**  
	 * Save's a picture object
	 * @return true if saved succesfully else return false 
	 */		
	function save() {
		if($this->id) {
			if($this->db->update('picture', $this)) {
				return true;
			};
		} else {
			if($this->db->insert('picture', $this)) {
				return true;
			};
		}
		return false;
	}
}

/**
 * Picturelist represents  Picturelist and belongs to an Account
 */
class PictureList extends Model {
	
	/**
	 * constructs parent
	 */		
	function __construct() {
		parent::__contruct();
	}

	/**  
	 * Gets an List of pictures that are not bought by given account
	 * @param account_id The id of the Account
	 * @return PictureList objectarray
	 */		
	function getNotBoughtByAccountId($account_id) {
		$sth = $this->db->prepare('SELECT p.* FROM picture p 
								   INNER JOIN account a ON p.account_id = a.id					   
								   WHERE a.id = :account_id
								   AND p.is_bought = 0');
		$sth->execute(array(':account_id' => $account_id));
		return $sth->fetchAll(PDO::FETCH_CLASS, 'Picture');		
	}

	/**  
	 * Gets an List of pictures that are bought by given account
	 * @param account_id The id of the Account
	 * @return PictureList objectarray
	 */		
	function getBoughtByAccountId($account_id) {
		$sth = $this->db->prepare('SELECT p.* FROM picture p 
								   INNER JOIN account a ON p.account_id = a.id					   
								   WHERE a.id = :account_id
								   AND p.is_bought = 1');
		$sth->execute(array(':account_id' => $account_id));
		return $sth->fetchAll(PDO::FETCH_CLASS, 'Picture');		
	}
}